package com.wf.course.web.controller.comm;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import com.utils.page.PageBean;
import com.wf.course.model.comm.ImgInfo;
import com.wf.course.service.IImgInfoService;
import com.wf.course.web.Constants;
import com.wf.course.web.OSSUtil;
import com.wf.course.web.Interceptor.LogInfo;

/**
 *图片管理
 */
@RequestMapping("imgInfo")
@Controller
public class ImgInfoController {

	@Autowired
	private IImgInfoService imgService;

	
	 /***
	  * 图片管理
	  * @param pageNo
	  * @param pageSize
	  * @param title
	  * @param model
	  * @return
	  */
	@LogInfo("查询图片信息列表")
	@RequestMapping("imgList")
	public String imgList(@RequestParam(value = "pageNo", required = false) Integer pageNo,
						  @RequestParam(value = "pageSize", required = false) Integer pageSize,
						  @RequestParam(value = "imgName", required = false) String imgName,
						  Model model){
		Map<String,Object> map=new HashMap<String,Object>(); 
		map.put("imgName",imgName);
		
		PageBean<ImgInfo> pageBean = new PageBean<>(pageNo, pageSize,null,map);
		imgService.getImgsList(pageBean);
		pageBean.setUrl("/imgInfo/imgList");
		model.addAttribute("page", pageBean);
		return "comm/imgList";
	}
	
	/**
	 * 图片：新增修改页面信息加载
	 * @param id
	 * @return
	 */
	@LogInfo("新增/修改图片信息，页面数据初始化")
	@RequestMapping("downLoadImg/{id}")
	public String downLoadImg(@PathVariable("id") Integer id,Model model){
		 if(id!=null && id!=0){
			 ImgInfo img = this.imgService.downLoadImg(id);
				model.addAttribute("img", img);
				model.addAttribute("type", "upd");
		 }else{
			 model.addAttribute("type", "add");
		 }
		 
		return "comm/imgEdit";		
	}
	
	
	/**
	 * 添加或修改
	 * @param eval
	 * @param logoFile
	 * @param newsFile
	 * @param request
	 * @return
	 */
	@LogInfo("添加或修改图片信息")
	@RequestMapping(value = {"imgEdit" } )
	public String imgEdit(@ModelAttribute ImgInfo img, 
			@RequestParam(value = "imgFile", required = false)MultipartFile imgFile, 
			HttpServletRequest request){
		 
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			img.setCreateTime(sdf.format(new Date()));

			//添加信息图片
			if(img.getId()==null){
				if (!((MultipartFile) imgFile).isEmpty()) {
					String newsUrl = "";
					try {
						newsUrl = OSSUtil.addFile(imgFile, "eval", Constants.IGM_BUCKET);
					} catch (IOException e) {
						e.printStackTrace();
					}
					img.setImgUrl(Constants.OSSURL+newsUrl);
				}
				//保存
				imgService.saveImg(img);
			}else{
				//修改
				//更新logoUrl
				if (imgFile.getSize() != 0) {
					ImgInfo imga = imgService.downLoadImg(img.getId());
					try{
						if(StringUtils.isNotBlank(imga.getImgUrl())){
							// 删除原来图片
							OSSUtil.delete(imga.getImgUrl(), Constants.IGM_BUCKET);
						}
					}catch(Exception e){
						System.out.println(e.getMessage());
					}
					// 重新上传
					String logoUrl = "";
					try {
						logoUrl = OSSUtil.addFile(imgFile, "eval", Constants.IGM_BUCKET);
					} catch (IOException e) {
						e.printStackTrace();
					}
					img.setImgUrl(Constants.OSSURL+logoUrl);
				}
				//修改
				imgService.updateImg(img);
				
			}
			 
		 
		return "redirect:/imgInfo/imgList";
		
	}
	
	/**
	 * 删除图片
	 * @param id
	 * @return
	 */
	@LogInfo("删除图片信息")
	@RequestMapping(value="deleteImg/{id}"  )
	public String deleteImg(@PathVariable Integer id){
			this.imgService.deleteImg(id);
			
		return "redirect:/imgInfo/imgList";
	}
	
}
